import 'package:flutter/material.dart';
import 'package:xws/helpers/my_selector.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/ui/mine/account/edit_header.dart';
import 'package:xws/ui/splash/splash_page.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/storage_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/safe_scaffold.dart';

import 'edit_description.dart';
import 'edit_gender.dart';
import 'edit_nikname.dart';

enum _AccountFuncs {
  header,
  nikname,
  signature,
  // address,
  gender,
}

List<String> _funcText = <String>[
  '头像',
  '昵称',
  '简介',
  // '常用地址',
  '性别',
];

extension _ExAccFunc on _AccountFuncs {
  String get name => _funcText[index];
}

/// 账户相关
class AccountPage extends StatelessWidget {
  const AccountPage({Key? key}) : super(key: key);

  /// 点击
  Future<void> _onTap(BuildContext context, _AccountFuncs af) async {
    switch (af) {
      case _AccountFuncs.header:
        await editHeader(context);
        break;
      case _AccountFuncs.nikname:
        await editNikname(context);
        break;
      case _AccountFuncs.signature:
        await editDescription(context);
        break;
      case _AccountFuncs.gender:
        await editGender(context);
        break;
      default:
    }
  }

  /// 删除账户
  Future<void> _delAccount(BuildContext context) async {
    final bool? r = await tips('确认删除账户?');

    if (r == null || !r) return;

    loading(context);
    final bool res = await delAccount();
    close(context);

    if (res) {
      setV('token', null);
      setV('phone', null);
      push(context, const SplashPage());
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      titleText: '账户',
      resizeToAvoidBottomInset: false,
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: _AccountFuncs.values
                    .map(
                      (_AccountFuncs af) => AppBtn(
                        onTap: () async => await _onTap(context, af),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              af.name,
                              style: TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.bold,
                                color: black(context).withOpacity(0.7),
                              ),
                            ),
                            _buildTail(context, af),
                          ],
                        ),
                        padding: const EdgeInsets.symmetric(horizontal: 20),
                        alignment: Alignment.center,
                        color: white(context),
                        height: 56,
                        radius: 0,
                      ),
                    )
                    .toList(),
              ),
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: AppBtn(
                onTap: () => _delAccount(context),
                child: Text(
                  '删除账户',
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                    color: white(context),
                  ),
                ),
                padding: const EdgeInsets.symmetric(horizontal: 20),
                alignment: Alignment.center,
                color: red(context),
                height: 50,
                radius: 10,
              ),
            ),
            const SizedBox(height: 50),
          ],
        ),
      ),
    );
  }

  /// 尾部
  Widget _buildTail(BuildContext context, _AccountFuncs af) {
    switch (af) {
      case _AccountFuncs.header:
        return Ms<UserState, String>(
          selector: (UserState u) => u.info.avatarUrl,
          builder: (String url) {
            return AppImg.fitCover(
              url: url,
              width: 30,
              height: 30,
              radius: 10,
            );
          },
        );

      case _AccountFuncs.nikname:
        return Ms<UserState, String>(
          selector: (UserState u) => u.info.nickName,
          builder: (String n) {
            return Text(
              n,
              style: TextStyle(
                fontSize: 14,
                color: black(context).withOpacity(0.7),
              ),
            );
          },
        );

      case _AccountFuncs.signature:
        return Ms<UserState, String>(
          selector: (UserState u) => u.info.description,
          builder: (String des) {
            return Text(
              des,
              style: TextStyle(
                fontSize: 14,
                color: black(context).withOpacity(0.7),
              ),
            );
          },
        );

      case _AccountFuncs.gender:
        return Ms<UserState, String>(
          selector: (UserState u) => u.info.gender,
          builder: (String g) {
            return Text(
              g,
              style: TextStyle(
                fontSize: 14,
                color: black(context).withOpacity(0.7),
              ),
            );
          },
        );

      default:
    }

    return const SizedBox.shrink();
  }
}
